package com.zegome.game.gomestory.main.helper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.zegome.game.gomestory.MainActivity;
import com.zegome.game.gomestory.TakePhotoActivity;
import com.zegome.game.gomestory.main.Center;
import com.zegome.game.gomestory.main.helper.network.AsyncTaskNetWork;
import com.zegome.game.gomestory.main.helper.network.IByteAsync;
import com.zegome.game.gomestory.main.helper.network.IGetSession;
import com.zegome.game.gomestory.main.helper.network.INetWorkAction;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.screen.GameScreen;

public class AndroidHelper {

	Center ctr;
	public AdmobControl admobControl;
	public MainActivity activity;
	public EditText inputText;
	public Vector<Integer> hiKeyBoard;
	public boolean isShowKeyBoard;
	public Toast toast;
	public String messToast="";	
	
	public boolean isChristmasWeek(){  		 
		Calendar calendar = Calendar.getInstance();
		int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);  
		log("DAY OF YEAR "+dayOfYear + "IS ON CHRISTMAS "+(dayOfYear>356?true:false));
		if(dayOfYear>356){
			return true;
		}
		return false;		
	}
	
	public int getVersionCode(){
		try {
			return activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			return -1;
		}
	}
	
	public void openGooglePlay(String appPackageName){		
		try {
		    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
		} catch (android.content.ActivityNotFoundException anfe) {
		    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
		}
	}
	
	public void openlink(final String link){
		myHandler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Intent i = new Intent(Intent.ACTION_VIEW, 
					       Uri.parse(link));
					activity.startActivity(i);
			}
		});
	}
	
	public void controlAdmob(boolean isShow){
		if(MainActivity.isAdmob){
			if(ctr.a.isHasDialog() && ctr.a.isFadeScreen == false){
				if(ctr.androidHelper.admobControl.isShow()){
					ctr.androidHelper.admobControl.hide();
				}			
		
			}
			else{
				if(ctr.androidHelper.admobControl.isHide()){
					ctr.androidHelper.admobControl.show();
				}	
			}
		}
	}
	public AndroidHelper(Center ctr){
		this.ctr = ctr;
	}
	
	public Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					if(inputText!=null){
						inputText.setVisibility(View.VISIBLE);						
						//inputText.setText(ctr.stageHelper.currentTextField.getText());
						inputText.setFocusable(true);
						inputText.requestFocus();						
						inputText.performClick();
					}
					break;
				case 1:
					if(inputText!=null){
						inputText.setVisibility(View.INVISIBLE);						
					}
					break;
				case 2:
					Intent myIntent = new Intent(activity, TakePhotoActivity.class);
					activity.startActivity(myIntent);							
	                break;
				case 3:
					if(toast==null)
						toast = Toast.makeText(activity, messToast, Toast.LENGTH_LONG);
					toast.setText(messToast);
					toast.show();
					break;
				}
		}
	};

	public void showToast(String str){
		messToast = str;
		myHandler.sendEmptyMessage(3);
	}
	
	public void hideEditText(){
		myHandler.sendEmptyMessage(1);
	}
	
	public void showEditText(){
		myHandler.sendEmptyMessage(0);
	}

	public void clear(){
		activity = null;
		inputText = null;
		isShowKeyBoard=false;
		sessionId = null;
	}
	
	public void showDialogPickPhoto(){            
		myHandler.sendEmptyMessage(2);
	}
	
	public boolean checkFileExist(String name){
		File wallpaperDirectory = new File(name);
		return wallpaperDirectory.exists();
	}
	
	public void initFolderAvatar(){
			try {
				File wallpaperDirectory = new File("/sdcard/data/avatar/");
				if(!wallpaperDirectory.exists())
				{				
					wallpaperDirectory.mkdirs();
				}			
			}
			catch(Exception ex){}
	}
			
	
	public void saveFile(String name, String data)
	{	
		try {
			File wallpaperDirectory = new File("/sdcard/data/ogme/");
			if(!wallpaperDirectory.exists())
			{				
				wallpaperDirectory.mkdirs();
			}			
			File myFile = new File("/sdcard/data/ogme/"+name);			
			myFile.createNewFile();
			FileOutputStream fOut = new FileOutputStream(myFile);
			OutputStreamWriter myOutWriter = 
									new OutputStreamWriter(fOut);
			myOutWriter.append(data);
			myOutWriter.close();
			fOut.close();
		} catch (Exception e) {
		}
	}
	
	public String getStringFile(String name, String def)
	{		
		String result = def;
		String data = null;
		try {
			File myFile = new File("/sdcard/data/ogme/"+name);
			
			FileInputStream fIn = new FileInputStream(myFile);
			
			BufferedReader myReader = new BufferedReader(
					new InputStreamReader(fIn));
			String aDataRow = "";
			String aBuffer = "";
			while ((aDataRow = myReader.readLine()) != null) {
				aBuffer += aDataRow;
			}
			data = aBuffer;
			myReader.close();
		} catch (Exception e) {
		}
		if(data!=null){
			result = data;
		}
		return result;
	}
	
	
	 public String getEmail() {
		    AccountManager accountManager = AccountManager.get(activity.getBaseContext()); 
		    Account account = getAccount(accountManager);		    
		    if (account == null) {
		      return "";
		    } else {
		      return account.name;
		    }
	}
	
    public  Account getAccount(AccountManager accountManager) {
	    Account[] accounts = accountManager.getAccountsByType("com.google");
	    Account account;
	    if (accounts.length > 0) {
	      account = accounts[0];      
	    } else {
	      account = null;
	    }
	    return account;
	  }
	
	 public boolean isNetworkAvailable() {
	        ConnectivityManager connectivityManager 
	              = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
	        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
	  }
	  
	
	public void init(){
		sessionId = null;
		httpclient = new DefaultHttpClient();
	}
	
	//==================================================================================================
		public final String els_url = "http://zegome.com/service/index/";
				//"http://192.168.0.111/zegome-service/Resources/version-1/index/";
		public final String els_url_resource = "http://zegome.com/service/";
				//"http://192.168.0.111/zegome-service/Resources/version-1/";
		public String sessionId;	
		public DefaultHttpClient httpclient;
		
		public void getSessionId(){
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient, null);
			net.setSession( new IGetSession() {
				
				@Override
				public HttpResponse send() {
					// TODO Auto-generated method stub
					final HttpGet httpget = new HttpGet(els_url + "login");
					HttpResponse response = null;
					try {
						response = httpclient.execute(httpget);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
						ctr.d.isSendingGetSessionId = false;
					}				
					return response;
				}
				
				@Override
				public void receive(HttpResponse response) {
					// TODO Auto-generated method stub
					if(response == null)
					{
						ctr.d.isSendingGetSessionId = false;
						return;
					}
			        List<Cookie> cookies = null;
			        if(httpclient!=null){
			        	cookies = httpclient.getCookieStore().getCookies();
			        }
			        if(cookies!=null){
			        if (cookies.isEmpty()) {
			        	log("Cookie is Empty");			       
			        }else {
			            for (int i = 0; i < cookies.size(); i++) {
			            	if(cookies.get(i). getName().equals("PHPSESSID" ))
			            	{
			            		sessionId = cookies.get(i).getValue ();
			            		if(sessionId != "")
			            		{
			            			log("Get Session: " + sessionId);			            			
			            		}
			            	}
			            }			            
			        }	
				}			        
			        ctr.d.isSendingGetSessionId = false;
			        ctr.d.isHasSessionId = true;
				}
			});		
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.sendGetSession();
				}
			});
		}
		
		public void sendTopUser(final BaseGameScreen screen, final Stage stage,final TweenManager tweenManager)
		{								
			
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "get-topuser");
							
						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("limit", "10"));
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);
									
											
										    HttpResponse response = null;
											//while (response == null) {								
											
										    try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}
										//}
											ctr.stageHelper.dimissDialogLoading();
											if(response==null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
									            responseText = convertStreamToString(instream);
									            					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
							return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							if(responseText==null){
								ctr.stageHelper.dimissDialogLoading();
								ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1, null, null);
								return;
							}
							try{
								JSONObject jsObj = new JSONObject(responseText);
								// TODO Auto-generated method stub
																										
									 if(jsObj.getString("code").equals("1")){
										 										
										 
										 JSONObject content = jsObj.getJSONObject("content");
										 										 
										 final JSONArray users = content.getJSONArray("top_user");
										 											 
										 //JSONObject giftsDaily = content.getJSONObject("daily_gift");										
										 
										 screen.postRunnable(new Runnable() {
											
											@Override
											public void run() {
												// TODO Auto-generated method stub												 
												ctr.stageHelper.createDialogTopPlayer(screen, users, stage, tweenManager);
											}
										});
									 }
								}
							catch(Exception ex){
								log("LOI TOP USER "+ex.getMessage());
							}
				            log("Response Top User: " + responseText); 					
						}
					});		
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		
		public void sendEndGameVS(final Stage stage,final TweenManager tweenManager, final String boss, final boolean isWin,final IMAction success,final IMAction error)
		{	
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "update-result");
							
						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("opponient", boss));
						     nvps.add(new BasicNameValuePair("result", isWin?"1":"3"));
						     //nvps.add(new BasicNameValuePair("gome", "100"));
						     if(ctr.d.typeBet == 1){
						    	 nvps.add(new BasicNameValuePair("gome", ctr.d.betNum+""));
						    	 Log.e("ENDGMAE","1");
						     }
						     else{
						    	 nvps.add(new BasicNameValuePair("gome", "0"));
						    	 Log.e("ENDGAME", "2");
						     }
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     
						     Log.e("abcknfsnflds", "Request: boss = " +boss + " | isWin = " +isWin + " | gome " + (ctr.d.typeBet == 1 ? ctr.d.betNum : 0));
						     
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);
										
											
										    HttpResponse response = null;
				
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
										
											if(response==null){
												
												return null;
											}
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
									            responseText = convertStreamToString(instream);
									            					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
							return responseText;							
						}
						
						@Override
						public void receive(final String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
							 
							if(responseText==null){
								
								//ctr.stageHelper.createDialog(stage, tweenManager, "Không thể kết nối! Vui lòng kiểm tra lại mạng trên thiết bị!", 1, null, null);
								ctr.d.vs2ResultAction = new IMAction() {
									
									@Override
									public void action() {
										// TODO Auto-generated method stub
										ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1,new ChangeListener() {													
											@Override
											public void changed(ChangeEvent arg0, Actor arg1) {
												// TODO Auto-generated method stub
												error.action();
											}
										},null);
									}
								};
								return;
							}
														
							
								
							ctr.d.vs2ResultAction = new IMAction() {
								
								@Override
								public void action() {
									// TODO Auto-generated method stub
									if(ctr.d.typeBet == 2 && isWin){
										ctr.d.money += ctr.d.betNum*2*0.7f;						
									}
									
									try{
									JSONObject jsObj = new JSONObject(responseText);
									// TODO Auto-generated method stub
																											
										 if(jsObj.getString("code").equals("1")){									 										
											 JSONObject content = jsObj.getJSONObject("content");
											 final JSONObject me = content.getJSONObject("user");
											 ctr.d.level = me.getInt("level");
											 ctr.d.exp = me.getInt("experiment");
											 ctr.d.experUp = me.getInt("experiment_up");
											 ctr.d.achievement = me.getString("achievement");
											 ctr.d.star = me.getInt("star");
											 ctr.d.moneyOnline = me.getInt("user_gome");
											 ctr.d.receive = me.getInt("user_gome_recieved");
											 ctr.d.thang_cap = me.getInt("thangcap");
											 //if(ctr.d.thang_cap>0)
												 ctr.d.coupon = me.getString("coupon");
											 
											 
											 final JSONObject boss = content.getJSONObject("opponient");	
											 ctr.d.levelB = boss.getInt("level");
											 ctr.d.expB = boss.getInt("experiment");
											 ctr.d.experUpB = boss.getInt("experiment_up");
											 ctr.d.achievementB = boss.getString("achievement");
											 ctr.d.starB = boss.getInt("star");
											 ctr.d.gomeB = boss.getInt("user_gome");
											 ctr.d.receiveB = boss.getInt("user_gome_recieved");
											 
											 
										 }
									}
									catch(Exception ex){
										
									}
									
									if(isWin){
										ctr.d.total_win++;								
									}
									else{					
										ctr.d.total_lose ++;
									}
									ctr.d.saveAccount();
							            success.action();
								}
							};
								
								
							
					            log("Response End Game VS: " + responseText); 	
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendBonusExp(final Stage stage,final TweenManager tweenManager)
		{					
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "bonus-experiment");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("type", "1"));
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);																														
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response == null)
												return null;
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
								if(responseText==null){
									ctr.stageHelper.dimissDialogLoading();
									//ctr.stageHelper.createDialog(stage, tweenManager, "Không thể kết nối! Vui lòng kiểm tra lại mạng trên thiết bị!", 1, null, null);
									return;
								}											
								try{
							    JSONObject result = new JSONObject(responseText);							    
					            if(result.getString("code").equals("1")){					         
					            	final JSONObject content = result.getJSONObject("content");							            	
					            	ctr.d.level = content.getInt("level");							            	
					            	ctr.d.achievement  = content.getString("achievement");
					            	ctr.d.star = content.getInt("star");
									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.bonusExp, 1, null, null);									
					            } 
								}
								catch(Exception ex){									
								}
					            log("Response Bonus Exp: " + responseText); 		
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendGift(final Stage stage,final TweenManager tweenManager, final int id,final int gold, final String coupon)
		{			
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "confirm-gift");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("gift_id", id+""));		    
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);
									
										
											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response==null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
								if(responseText==null){
									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1, null, null);
									return;
								}
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){
					            	if(gold>0){
					            	ctr.d.moneyOnline += gold;
					            	ctr.d.saveAccount();
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getMoneyCharge(gold), 1, null, null);					            	
					            	}
					            	else{
					            		ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.receiveCouponGift, 2,
					            				new ChangeListener() {													
													@Override
													public void changed(ChangeEvent arg0, Actor arg1) {
														// TODO Auto-generated method stub														
														ctr.stageHelper.createDialogCoupon(stage, tweenManager, coupon);
													}
												},
					            				null);
					            	}
					            }
					            else{	
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.error+": "+ result.getString("content"), 1, null, null);					            	
					            }
					            }
					            catch(Exception ex){
					            	
					            }
					            log("Response Confirm Gift: " + responseText); 		
						}
					});
			
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendGetNotice()
		{			
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "get-notice");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     nvps.add(new BasicNameValuePair("platform", "1"));
						     
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response == null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
											
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    log("Response Get Notify Anonymous: " + responseText);
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
								if(responseText==null){
									ctr.d.isSendingCheckNotifyAnonymous = false;
									return;
								}
								ctr.androidHelper.saveFile("notify", "");
								ctr.d.isCheckNotifyAnonymous = true;
								ctr.d.isSendingCheckNotifyAnonymous = false;
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){					            	
					            	JSONObject content = result.getJSONObject("content");					            	
									JSONArray messages = content.getJSONArray("message");									 
									JSONArray advertise = content.getJSONArray("advertise");
									ctr.autoRunText.notify.removeAllElements();
									ctr.d.gameAdvertiseLoad.removeAllElements();
									 for(int i=0; i<messages.length(); i++){
										 JSONObject ele = messages.getJSONObject(i);
										 ctr.autoRunText.addNotify(0,ctr.d.isVietnamese?ele.getString("message_content"):ele.getString("message_eng"),null,null);
									 }
									 
									 for(int i=0; i<advertise.length(); i++){
										 JSONObject ele = advertise.getJSONObject(i);
										 //ctr.a.notify.add(ele.getString("gift_id") +"-"+ele.getString("user_send")+"-"+ele.getString("zegome_gold")+"-"+ele.getString("coupon")+"-"+ele.getString("message"));									 
										 ctr.d.gameAdvertiseLoad.add(new GameAdvertise(i,ele.getString("icon"), ele.getString("link"),ctr.d.isVietnamese?ele.getString("message_vn"):ele.getString("message_eng")));
									 }
					            }					            
					            }
					            catch(Exception ex){					            	
					            	log(ex.getMessage());
					            }		
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		
		public void sendCheckVersion()
		{		
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "get-version");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     //nvps.add(new BasicNameValuePair("platform", "1"));
						     
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									//httpost.setHeader("PHPSESSID",sessionId);											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response == null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
											
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    log("Response Check Version: " + responseText);
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							ctr.stageHelper.dimissDialogLoading();
							// TODO Auto-generated method stub
								if(responseText==null){
									ctr.d.isSendingCheckVersion = false;
									return;
								}
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){					            	
					            	JSONObject content = result.getJSONObject("content");					            	
					            	int lastestVersion = content.getInt("version");
					            	if(ctr.d.currentVersion < lastestVersion){					            		
					            		ctr.d.isVersionLatest = false;
					            	}
					            	else{
					            		ctr.d.isVersionLatest = true;
					            	}					            	
					            	ctr.d.messageUpdate = ctr.d.isVietnamese?content.getString("message_vn"):content.getString("message_eng");
					            	ctr.d.packageName = content.getString("package_name");					            	
					            	ctr.d.saveIsLatestVersion(lastestVersion);
					            }					            
					            }
					            catch(Exception ex){					            	
					            	log(ex.getMessage());
					            }	
								ctr.d.isCheckVersion = true;
								ctr.d.isSendingCheckVersion = false;
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendMail()
		{		
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub\
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "save-mail");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("mail", ctr.d.emailPre));
						     //nvps.add(new BasicNameValuePair("platform", "1"));
						     
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									//httpost.setHeader("PHPSESSID",sessionId);											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response == null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
											
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    log("Response Save mail: " + responseText);
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							ctr.stageHelper.dimissDialogLoading();
							// TODO Auto-generated method stub
								if(responseText==null){								
									ctr.d.isSendingEmail = false;
									return;
								}
								else{
									log("Send mail: Ok" );
									ctr.d.saveSendEmail();
									ctr.d.isSendEmail = true;
									ctr.d.isSendingEmail = false;
								}
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendPayMoneyGome(final Stage stage,final TweenManager tweenManager,final int money,final IMAction success)
		{						
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "add-gome");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     nvps.add(new BasicNameValuePair("platform", "1"));
						     nvps.add(new BasicNameValuePair("gome", money+""));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response == null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
											
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    log("Response Add Gome: " + responseText);
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
								if(responseText==null){
									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1, null, null);
									return;
								}
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){					            	
					            	JSONObject content = result.getJSONObject("content");
					            	ctr.d.moneyOnline = content.getInt("user_gome");
					            	ctr.d.saveAccount();
					            	//ctr.d.transaction_id = content.getString("transaction_id");
					            	//ctr.stageHelper.createDialogApotta(stage, tweenManager);
					            	success.action();
					            }
					            else{	
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.error+": "+ result.getString("content"), 1, null, null);					            	
					            }
					            }
					            catch(Exception ex){					            	
					            	log(ex.getMessage());
					            }		
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendStartTransaction(final Stage stage,final TweenManager tweenManager)
		{			
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "create-transaction");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     nvps.add(new BasicNameValuePair("platform", "1"));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response == null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
											
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    log("Response Create Transaction: " + responseText);
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
								if(responseText==null){
									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1, null, null);
									return;
								}
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){					            	
					            	JSONObject content = result.getJSONObject("content");
					            	ctr.d.transaction_id = content.getString("transaction_id");
					            	ctr.stageHelper.createDialogApotta(stage, tweenManager);
					            }
					            else{	
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.error+": "+ result.getString("content"), 1, null, null);					            	
					            }
					            }
					            catch(Exception ex){
					            	
					            }
					            		
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendFinishTransaction(final Stage stage,final TweenManager tweenManager, final String amount, final String cur)
		{						
			ctr.stageHelper.createDialogLoading(stage, tweenManager);
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "finish-transaction");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("transaction_id", ctr.d.transaction_id));
						     nvps.add(new BasicNameValuePair("code", "1"));
						     nvps.add(new BasicNameValuePair("message", ctr.d.transaction_id+"|"+amount+"|"+cur));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												e.printStackTrace();						
											} 						
											if(response ==null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
								if(responseText==null){
									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1, null, null);
									return;
								}
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){					            	
					            	JSONObject content = result.getJSONObject("content");
					            	ctr.d.moneyOnline = content.getInt("user_gome");					            	
					            	ctr.d.saveAccount();
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getCurrentMoney(), 1, new ChangeListener() {										
										@Override
										public void changed(ChangeEvent arg0, Actor arg1) {
											// TODO Auto-generated method stub
											ctr.androidHelper.sendBonusExp(stage, tweenManager);
										}
									}, null);
					            }
					            else{	
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.error+": "+ result.getString("content"), 1, null, null);					            						            	
					            }
					            }
					            catch(Exception ex){
					            	
					            }
					            log("Response Finish Transaction: " + responseText); 		
						}
					});
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendCoupon(final Stage stage,final TweenManager tweenManager, final String value)
		{		
			ctr.stageHelper.createDialogLoading(stage, tweenManager);			
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "use-coupon");

						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("coupon_value", value));		    
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);
																				
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();						
											}						
											if(response==null){
												//ctr.stageHelper.createDialog(stage, tweenManager, "Lối kết nối!", 1, null, null);
												return null;
											}
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    
									            responseText = convertStreamToString(instream);
										     					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							 ctr.stageHelper.dimissDialogLoading();
								if(responseText==null){
									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.connectError, 1, null, null);
									return;
								}
					            try{
					            JSONObject result = new JSONObject(responseText);
					            if(result.getString("code").equals("1")){
					            	JSONObject content = result.getJSONObject("content");
					            	int oldMoney = ctr.d.moneyOnline;
					            	ctr.d.moneyOnline =  content.getInt("user_gome");
					            	ctr.d.saveAccount();
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getMoneyCharge(ctr.d.moneyOnline-oldMoney), 1, null, null);					            	
					            }
					            else{	
					            	ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.error+": "+ result.getString("content"), 1, null, null);					            	
					            }
					            }
					            catch(Exception ex){
					            	
					            }
					            log("Response Confirm Coupon: " + responseText); 		
						}
					});
			
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		
		public void sendGetBoss(final BaseGameScreen screen)
		{			
			final AsyncTaskNetWork asyncGetBoss = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							
							String responseText = null;
														
							final HttpPost httpost = new HttpPost(els_url + "get-opponient");
							
						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     if(ctr.d.typeBet == 1)
						     nvps.add(new BasicNameValuePair("gome", ctr.d.betNum+""));
						     else{
						    	 nvps.add(new BasicNameValuePair("gome", "0"));
						     }
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
						    	 httpost.setHeader("PHPSESSID",sessionId);
													
											
										    HttpResponse response = null;
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												e.printStackTrace();						
											}
											if(response == null)
												return null;
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
									            responseText = convertStreamToString(instream);
									            					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    System.out.println("HRHRHRHRH"+responseText);
										    return responseText;
										   
						}
						
						@Override
						public void receive(final String responseText) {
							// TODO Auto-generated method stub		
							if(responseText==null){
								ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), ctr.lang.connectError, 1, null, null);
								return;
							}
							String link1 = "", name1 = "";
							try{
							JSONObject result_1 = new JSONObject(responseText);
							
				            if(result_1.getString("code").equals("1")){					         
				            	final JSONObject boss = result_1.getJSONObject("content");
				            	link1 = boss.getString("avatar");
				            	name1 = boss.getString("user_name");
				            }
							}
							catch(Exception ex){}
							final String link = link1;
							final String name = name1;
							
							screen.postRunnable(new Runnable() {								
								@Override
								public void run() {
									// TODO Auto-generated method stub
									final AsyncTaskNetWork newNet = new  AsyncTaskNetWork(httpclient,null);										 
									newNet.setByteAction(new IByteAsync() {											
											@Override
											public byte[] send() {
												// TODO Auto-generated method stub
												byte[] bytes = null;
												try{
													bytes = ctr.ioHelper.readBytes(OpenHttpConnection(els_url_resource+link));
												} catch (Exception e) {
													// TODO Auto-generated catch block
													//e.printStackTrace();
												}
												return bytes;																						
											}											
											@Override
											public void receive(final byte[] bytes) {
												// TODO Auto-generated method stub												
												 try{
											            JSONObject result = new JSONObject(responseText);
											            if(result.getString("code").equals("1")){					         
											            	final JSONObject boss = result.getJSONObject("content");
											            					            	   
									            			   											            
									    		     	    screen.postRunnable(new Runnable() {
																
																@Override
																public void run() {
																	// TODO Auto-generated method stub
																	try{
																		if(bytes!=null){
																	FileOutputStream out = new FileOutputStream("/sdcard/data/avatar/"+name);
																		out.write(bytes);
											    		     	       out.flush();
											    		     	       out.close();
																		
											    		     	      if(ctr.a.avatarBossTexture!=null)
											    							ctr.a.avatarBossTexture.dispose();
											    						ctr.a.avatarBossTexture = null;
											    						ctr.a.avatarBossTexture = new Texture(Gdx.files.external("data/avatar/"+name));	
											    						if(ctr.a.avatarBossTexture!=null){
											    							ctr.a.avatarBoss = new TextureRegion(ctr.a.avatarBossTexture);
											    						}															    		     	       
																	}
																	}
																	catch(Exception ex){
																		log("SendGetBoss"+ex.getMessage());
																	}
																	try{
																																	
																		ctr.d.nameB = boss.getString("user_name");
													            	ctr.d.levelB = boss.getInt("level");
													            	ctr.d.expB = boss.getInt("experiment");
													            	ctr.d.achievementB = boss.getString("achievement");
													            	ctr.d.starB = boss.getInt("star");
													            	ctr.d.gomeB = boss.getInt("user_gome");	
													            	ctr.d.total_winB = boss.getInt("total_win");
													            	ctr.d.total_loseB = boss.getInt("total_lose");
													            	
													            	ctr.d.speedB = 5-ctr.d.getTypeAchievement(ctr.d.achievementB);
													            	ctr.d.startB = 0.1f*(screen.ctr.d.speedB) + 0.5f;
													            	if (ctr.d.isLDPI) {
													            		ctr.d.factorB = 0.25f+(screen.ctr.d.speedB*0.025f);
													            	} else {
													            		ctr.d.factorB = 0.22f+(screen.ctr.d.speedB*0.02f);
													            	}													            	
													            	
													            	if(ctr.d.typeBet == 2){
													            		ctr.d.money -= ctr.d.betNum;
													            		ctr.d.saveAccount();
													            	}
													            	
													            	//ctr.d.achievementB = boss.getString("");
													            	log("SendGetBoss3"+ " "+ctr.d.nameB);
													            	}
													            	catch(Exception ex){
													            		log("SendGetBoss2"+ex.getMessage());
													            	
													            	}
																}
															});					            		            	
											            }					            
											            }
											            catch(Exception ex){					            	
											            	log(ex.getMessage());
											            }
											            log("Respone Request Boss: " + responseText); 	
											}
																			
									});
									
									if(checkFileExist("/sdcard/data/avatar/"+name)){
										
							            //log("Respone Request Boss has avatar: " + responseText); 	
							            //log("NAME SCREEN: " + screen.getName()); 
									screen.getController().getActivity().postRunnable(new Runnable() {

										@Override
										public void run() {
											// TODO Auto-generated method stub

											log(name + "VS KO LOAD");
											if (ctr.a.avatarBossTexture != null)
												ctr.a.avatarBossTexture
														.dispose();
											ctr.a.avatarBossTexture = null;
											ctr.a.avatarBossTexture = new Texture(
													Gdx.files
															.external("data/avatar/"
																	+ name));
											if (ctr.a.avatarBossTexture != null) {
												ctr.a.avatarBoss = new TextureRegion(
														ctr.a.avatarBossTexture);
											}

											try {
												JSONObject result = new JSONObject(
														responseText);
												if (result.getString("code")
														.equals("1")) {
													final JSONObject boss = result
															.getJSONObject("content");

													ctr.d.nameB = boss
															.getString("user_name");
													ctr.d.levelB = boss
															.getInt("level");
													ctr.d.expB = boss
															.getInt("experiment");
													ctr.d.achievementB = boss
															.getString("achievement");
													ctr.d.starB = boss
															.getInt("star");
													ctr.d.gomeB = boss
															.getInt("user_gome");
													ctr.d.total_winB = boss
															.getInt("total_win");
													ctr.d.total_loseB = boss
															.getInt("total_lose");
													ctr.d.speedB = 5 - ctr.d
															.getTypeAchievement(ctr.d.achievementB);
													ctr.d.startB = 0.1f * (screen.ctr.d.speedB) + 0.5f;
													ctr.d.factorB = 0.25f + (screen.ctr.d.speedB * 0.025f);

													if (ctr.d.typeBet == 2) {
														ctr.d.money -= ctr.d.betNum;
														ctr.d.saveAccount();
													}

												}
											} catch (Exception ex) {
											}
										}
									});
										}
									else{
									myHandler.post(new Runnable() {
										
										@Override
										public void run() {
											// TODO Auto-generated method stub
											newNet.sendByte();
										}
									});	
									}
								}
							});
						}
					});
			
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					asyncGetBoss.send();
				}
			});		
		}
		
		private InputStream OpenHttpConnection(String strURL) throws IOException{
			
			 InputStream inputStream = null;
			 URL url = new URL(strURL);
			 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			 if (Build.VERSION.SDK != null
					 && Build.VERSION.SDK_INT > 13) {
					 conn.setRequestProperty("Connection", "close");
					 }
			 try{
			 //conn.setRequestMethod("GET");
			 //conn.connect();
             //if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                 inputStream = conn.getInputStream();
             //}
             //System.out.println(strURL);
			 }
			 catch (Exception e) {
				// TODO: handle exception
				 e.printStackTrace();
			}
			 return inputStream;
			 
		}
		
		public void login(final IWMAction process) {			
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							 final HttpPost httpost = new HttpPost(els_url + "login");
						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("user_name", ctr.d.namePre));
						     nvps.add(new BasicNameValuePair("password", ctr.d.passPre));
						     nvps.add(new BasicNameValuePair("email", ctr.d.emailPre));
						     nvps.add(new BasicNameValuePair("platform", "1"));
						     nvps.add(new BasicNameValuePair("game_id", "1"));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
								 httpost.setHeader("PHPSESSID",sessionId);
						     }
						     catch(Exception ex){
						    	 
						     }
								    HttpResponse response = null;						    
									//while (response == null) {							
								    try {
										response = httpclient.execute(httpost);
									} catch (Exception e) {
										// TODO Auto-generated catch block
										//e.printStackTrace();
										ctr.d.isSendingLogin = false;
									}				
									//}	
								    if(response == null){
								    	ctr.d.isSendingLogin = false;
								    	return null;
								    }
								    int status_code = response.getStatusLine().getStatusCode();						    
								    //log("" + status_code);
								    HttpEntity entity = response.getEntity();
								    if (entity != null || status_code == 200 || status_code == 201) {
							        	InputStream instream = null;
										try {
											instream = entity.getContent();
										} catch (IllegalStateException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
							            responseText = convertStreamToString(instream);								 
							            try {
											instream.close();
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}							
								    }
								    
								    return responseText;
								   }
						@Override
						public void receive(String result) {
							// TODO Auto-generated method stub					
							if(result == null){
								ctr.stageHelper.dimissDialogLoading();
								return;								
							}
							ctr.androidHelper.saveFile("notify","");
							ctr.autoRunText.notify.removeAllElements();
							ctr.d.gameAdvertiseLoad.removeAllElements();
							  process.action(result);
					            ctr.d.isSendingLogin = false;
					            // Closing the input stream will trigger connection release
					            log("Response Login: " + result); 
						}
					});
			
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}
		public void register(final IWMAction process) {
			//http://zegome.com/service/index/create-account?user_name=phutran&password=123456&email=nguyen.trung.kien147@gmail.com&platform=2
			final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "create-account");
						     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						     nvps.add(new BasicNameValuePair("user_name", ctr.d.namePre));
						     nvps.add(new BasicNameValuePair("password", ctr.d.passPre));
						     nvps.add(new BasicNameValuePair("email", ctr.d.emailPre));
						     nvps.add(new BasicNameValuePair("platform", "1"));
						     //nvps.add(new BasicNameValuePair("game_id", "1"));
						     try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						     }
						     catch(Exception ex){}
						     //httpost.setHeader("PHPSESSID",sessionId);
										
											
										    HttpResponse response = null;
										   // while (response == null) {
																			
											try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											} 
										    //}
											if(response == null){
												return null;
											}
											
										    int status_code = response.getStatusLine().getStatusCode();						    
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
									            responseText = convertStreamToString(instream);					            
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							if(responseText == null){
								ctr.stageHelper.dimissDialogLoading();
								return;
								
							}
				            process.action(responseText);
				            log("Response Register: " + responseText); 
						}
					});	
			
			myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		}

		public void log(String string) {
			// TODO Auto-generated method stub
			if(string==null)
				return;
			Log.e("serverthienkien",string);
		}
		 public String convertStreamToString(InputStream is) {

		    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		        StringBuilder sb = new StringBuilder();

		        String line = null;
		        try {
		            while ((line = reader.readLine()) != null) {
		                sb.append(line + "\n");
		            }
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                is.close();
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
		        return sb.toString();
		    }
		 		 
		 public void sendCheckAvatar(final GameScreen screen){		
			 final AsyncTaskNetWork net  = new AsyncTaskNetWork(httpclient,
					 new INetWorkAction() {						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "get-avatar");
						    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						    nvps.add(new BasicNameValuePair("time_update_avatar", ctr.d.timeAvatar));
						    try{
						    	 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
								 httpost.setHeader("PHPSESSID",sessionId);
						     }
						     catch(Exception ex){
						    	 
						     }
							HttpResponse response = null;
						    //while (response == null) {																	
							try {
								response = httpclient.execute(httpost);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								ctr.d.isSendingAvatar = false;
							}
						    //}
							if(response == null)
							{
								ctr.d.isSendingAvatar = false;														
								return null;
							}
							
							int status_code = response.getStatusLine().getStatusCode();
						    //log("" + status_code);
						    HttpEntity entity = response.getEntity();
						    if (entity != null || status_code == 200 || status_code == 201) {
					        	InputStream instream = null;
								try {
									instream = entity.getContent();
								} catch (IllegalStateException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
					            responseText = convertStreamToString(instream);
					            // Closing the input stream will trigger connection release
					            try {
									instream.close();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
						    }
						    log("Response Avatar:"+responseText); 
							return responseText;
						}
						
						@Override
						public void receive(final String responseText) {
							// TODO Auto-generated method stub
							if(responseText==null || responseText.equals("")){
								//ctr.d.isSendingAvatar = false;
								return;
						}
							
							
							 try{
						            JSONObject result = new JSONObject(responseText);
						            if(result.getString("code").equals("1")){					         
						            	
						            	
						            	
						            	final JSONObject content = result.getJSONObject("content");							            	
						            	final String time = content.getString("time_update");							            	
						            	final String avatar = content.getString("avatar");
						            	
						            	if(time.equals("null") || avatar.equals("null")){
								            ctr.d.isSendingAvatar = false;
								            ctr.d.isCheckAvatar = true;		
						            		return;
						            	}
						            	
										final AsyncTaskNetWork newNet = new  AsyncTaskNetWork(httpclient,null);										 
										newNet.setByteAction(new IByteAsync() {											
												@Override
												public byte[] send() {
													// TODO Auto-generated method stub
													byte[] bytes = null;
													try{
														bytes = ctr.ioHelper.readBytes(OpenHttpConnection(els_url_resource+avatar));
													} catch (Exception e) {
														// TODO Auto-generated catch block
														//e.printStackTrace();
													}
													return bytes;
												}
												
												@Override
												public void receive(final byte[] bytes) {
													// TODO Auto-generated method stub
													 try{						            
										    		     	    screen.postRunnable(new Runnable() {
																	
																	@Override
																	public void run() {
																		// TODO Auto-generated method stub
																		if(bytes!=null){
																		try{
																		FileOutputStream out = new FileOutputStream("/sdcard/data/avatar/"+ctr.d.name);
																		out.write(bytes);
												    		     	       out.flush();
												    		     	       out.close();												    		     	       
												    		     	      if(ctr.a.avatarTexture!=null)
												    							ctr.a.avatarTexture.dispose();
												    						ctr.a.avatarTexture = null;
												    						ctr.a.avatarTexture = new Texture(Gdx.files.external("data/avatar/"+ctr.d.name));	
												    						if(ctr.a.avatarTexture!=null){
												    							ctr.a.avatarMe = new TextureRegion(ctr.a.avatarTexture);
												    						}															    		     	       
												    						ctr.stageHelper.avatar.setDrawable(new TextureRegionDrawable(ctr.a.avatarMe));
												    						ctr.d.timeAvatar = time;
												    						ctr.d.saveAvatarInfo();
																		}
																		catch(Exception ex){
																			log("SendCheckAvatar"+ex.getMessage());
																		}
																		}
																	}
																});					            		            				            				            
												            }
												            catch(Exception ex){					            	
												            	log(ex.getMessage());
												            }											
												}
																				
										});
										myHandler.post(new Runnable() {
											
											@Override
											public void run() {
												// TODO Auto-generated method stub
												newNet.sendByte();
											}
										});											            	
						            }								 				            
						            }
						            catch(Exception ex){			
						            	log("CheckAvatar"+ex.getMessage());
						            }						            
					            ctr.d.isSendingAvatar = false;
					            ctr.d.isCheckAvatar = true;					            
						}
					});	
			 myHandler.post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					net.send();
				}
			});
		 }
		 
		 public void sendNewAvatar(final String bytes){	
			 final AsyncTaskNetWork net = new AsyncTaskNetWork(httpclient,
					 new INetWorkAction() {
						
						@Override
						public String send() {
							// TODO Auto-generated method stub
							String responseText = null;
							final HttpPost httpost = new HttpPost(els_url + "save-avatar");
						    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
						    nvps.add(new BasicNameValuePair("avatar", bytes));
						    //nvps.add(new BasicNameValuePair("game_id", "1"));
						    try{
						    	httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
						    }
						    catch(Exception ex){}
									httpost.setHeader("PHPSESSID",sessionId);
									
									
											
										    HttpResponse response = null;
										//while (response == null) {
																			
										    try {
												response = httpclient.execute(httpost);
											} catch (Exception e) {
												// TODO Auto-generated catch block
												//e.printStackTrace();
												ctr.d.isSyncingNewAvatar = false;
											}
										//}
										    if(response == null){
										    	ctr.d.isSyncingNewAvatar = false;
										    	return null;
										    }
										    int status_code = response.getStatusLine().getStatusCode();
										    //log("" + status_code);
										    HttpEntity entity = response.getEntity();
										    if (entity != null || status_code == 200 || status_code == 201) {
									        	InputStream instream = null;
												try {
													instream = entity.getContent();
												} catch (IllegalStateException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
									            responseText = convertStreamToString(instream);
									            // Closing the input stream will trigger connection release
									            try {
													instream.close();
												} catch (IOException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
										    }
										    return responseText;
						}
						
						@Override
						public void receive(String responseText) {
							// TODO Auto-generated method stub
							if(responseText == null){								
								return;
						}
							  String time = null;
					            try{
						            JSONObject result = new JSONObject(responseText);
						            if(result.getString("code").equals("1")){					         
						            	JSONObject boss = result.getJSONObject("content");
						            	time = boss.getString("time_update");
						            	ctr.d.timeAvatar = time;
						            	ctr.d.isSyncingNewAvatar = false;
						            	ctr.d.isSyncedNewAvatar = true;
						            	ctr.d.isNewAvatarNotSync = false;								            	
						            	ctr.d.saveAvatarInfo();
						            }					            
						            }
						            catch(Exception ex){					            	
						            }
					            log("Response Sync Avatar: " + responseText );
					            	//	+"--time"+time); 
						    ctr.d.isSyncingNewAvatar = false;
						}
					});
			 
			 myHandler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						net.send();
					}
				});
			

		 }
		 		 
	//==================================================================================================			 
			public void sendGetImage(final BaseGameScreen screen, final LazyLoadImage lazy)
			{					
				ctr.d.isLoadingLazy = true;
				lazy.isSending = true;
				final AsyncTaskNetWork newNet = new  AsyncTaskNetWork(httpclient,null);										 
				newNet.setByteAction(new IByteAsync() {
						
						@Override
						public byte[] send() {
							// TODO Auto-generated method stub
							byte[] bytes = null;
							try{
								//log("http://zegome.com/service/"+lazy.link);
								
								bytes = ctr.ioHelper.readBytes(OpenHttpConnection(els_url_resource+lazy.link));								
							
							} catch (Exception e) {
								// TODO Auto-generated catch block								
							}
							return bytes;
						}
						
						@Override
						public void receive(final byte[] bytes) {
							// TODO Auto-generated method stub	
							if(bytes==null){
		    					lazy.isFinish = false;
		    					lazy.isSending = false;
		    					ctr.d.isLoadingLazy = false;
								return;
							}
				    		     	    screen.postRunnable(new Runnable() {
											
											@Override
											public void run() {
												// TODO Auto-generated method stub
												try{
												FileOutputStream out = new FileOutputStream("/sdcard/data/avatar/"+lazy.name);
						    		     	       out.write(bytes);
						    		     	       out.flush();
						    		     	       out.close();
						    		     	       
						    						lazy.load(screen.getTween());					    		     	       									
						    					lazy.isFinish = true;
						    					lazy.isSending = false;
						    					ctr.d.isLoadingLazy = false;
												}
												catch(Exception ex){
												}												
											}
										});					            		           			            						           
						}
														
				});
				
				 myHandler.post(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							newNet.sendByte();
						}
					});
			}		 
			//=============================================================
			public void sendGetAdvertise(final BaseGameScreen screen, final GameAdvertise lazy)
			{		
				ctr.d.isLoadingGameAdvertise = true;
				lazy.isSending = true;
				final AsyncTaskNetWork newNet = new  AsyncTaskNetWork(httpclient,null);										 
				newNet.setByteAction(new IByteAsync() {
						
						@Override
						public byte[] send() {
							// TODO Auto-generated method stub
							byte[] bytes = null;
							try{
								//log(els_url_resource+lazy.link);								
								bytes = ctr.ioHelper.readBytes(OpenHttpConnection(els_url_resource+lazy.link));															
							} catch (Exception e) {
								// TODO Auto-generated catch block		
								//log(e.getMessage());
							}
							return bytes;
						}
						
						@Override
						public void receive(final byte[] bytes) {
							// TODO Auto-generated method stub	
							if(bytes==null){
		    					lazy.isFinish = false;
		    					lazy.isSending = false;
		    					ctr.d.isLoadingGameAdvertise = false;
		    					
								return;
							}
				    		     	    screen.postRunnable(new Runnable() {
											
											@Override
											public void run() {
												// TODO Auto-generated method stub
												try{
												FileOutputStream out = new FileOutputStream("/sdcard/data/avatar/"+lazy.name);
						    		     	       out.write(bytes);
						    		     	       out.flush();
						    		     	       out.close();
						    		     	    //   System.out.println("LOAD OK"+lazy.link);
						    		     	    ctr.autoRunText.addNotify(1,lazy.content, "data/avatar/"+lazy.name, lazy.url);				    		     	       									
						    					lazy.isFinish = true;
						    					lazy.isSending = false;
						    					ctr.d.isLoadingGameAdvertise = false;
												}
												catch(Exception ex){
													log(ex.getMessage());
												}												
											}
										});					            		           			            						           
						}
														
				});
				
				 myHandler.post(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							newNet.sendByte();
						}
					});
			}	
}